<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>如何将Perl代码着色</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="Module.html">Module</a>&nbsp;&nbsp;>>Next: <a href="ChineseCoding.html">汉字编码笔记</a></p>
<h1>如何将Perl代码着色</h1>
<div class='content'>
<p>Category: <a href='Modules.html'>Modules</a> &nbsp; Keywords: <b>color highlight perl</b></p><h2>先决条件</h2>
cpan Syntax::Highlight::Perl

<h2>代码解释</h2>
perldoc <a href='http://search.cpan.org/perldoc?Syntax::Highlight::Perl'>Syntax::Highlight::Perl</a><br>
perldoc里或许解释清楚了，但是蛮长了，我没耐心看。使用里面的代码一点反应都没有。
<pre>
use Syntax::Highlight::Perl;

my $formatter = new Syntax::Highlight::Perl;
print $formatter->format_string($my_string);
</pre>
不想仔细看完全文，最主要是我懒，看英文头痛得很。<br>
还好可以Search, 找到了<a href="http://sedition.com/perl/perl-colorizer.html">Coloring perl code in HTML</a>。<br>
对着那代码，改写了些许颜色。将它span里改写成使用class而不是style.这样比较容易知道哪些词是属于哪一部分。<br>
完成后的代码如下。现炒现卖，直接将它着色。<br>
打算迟些时候将它做为<a href="Epanlet.html">Eplanet</a>的新功能。<p>
<link rel="stylesheet" href="highlightperl.css" type="text/css" />
highlightperl.css - 此CSS文档可随意更改。
<pre>
.vs { color:#080; }
.va { color:#f70; }
.vh { color:#80f; }
.vt { color:#f03; }
.sub { color:#980; }
.qr { color:#ff8000; }
.str { color:#000; }
.cm { color:#008080;font-style:italic; }
.cmp { color:#014;font-family: garamond,serif;font-size:11pt; }
.bw { color:#3A3; }
.pk { color:#900; }
.nb { color:#f0f; }
.op { color:#000; }
.sym { color:#000; }
.kw { color:#00f; }
.bo { color:#f00; }
.bf { color:#001; }
.char { color:#800; }
.dr { color:#399;font-style:italic; }
.lb { color:#939;font-style:italic; }
.ln { color:#000; }
</pre>
highlight.pl
<pre>
<span class="dr">#!/usr/bin/perl -T</span>
<span class="kw">use</span> <span class="pk">strict</span><span class="sym">;</span>
<span class="kw">use</span> <span class="pk">warnings</span><span class="sym">;</span>
<span class="kw">use</span> <span class="pk">CGI::Carp</span> <span class="qr">qw(</span><span class="str">fatalsToBrowser</span><span class="qr">)</span><span class="sym">;</span>
<span class="kw">use</span> <span class="pk">CGI</span> <span class="qr">qw/</span><span class="str">:standard</span><span class="qr">/</span><span class="sym">;</span>

<span class="kw">my</span> <span class="vs">$cgi</span> <span class="op">=</span> <span class="bw">new</span> <span class="bw">CGI</span><span class="sym">;</span>
<span class="bo">print</span> <span class="vs">$cgi</span><span class="op">-></span><span class="sub">header</span><span class="sym">;</span>

<span class="kw">use</span> <span class="pk">Syntax::Highlight::Perl</span><span class="sym">;</span>

<span class="kw">my</span> <span class="vs">$color_table</span> <span class="op">=</span> <span class="sym">{</span>
    <span class="qr">'</span><span class="str">Variable_Scalar</span><span class="qr">'</span>   <span class="op">=></span> <span class="qr">'</span><span class="str">vs</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Variable_Array</span><span class="qr">'</span>    <span class="op">=></span> <span class="qr">'</span><span class="str">va</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Variable_Hash</span><span class="qr">'</span>     <span class="op">=></span> <span class="qr">'</span><span class="str">vh</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Variable_Typeglob</span><span class="qr">'</span> <span class="op">=></span> <span class="qr">'</span><span class="str">vt</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Subroutine</span><span class="qr">'</span>        <span class="op">=></span> <span class="qr">'</span><span class="str">sub</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Quote</span><span class="qr">'</span>             <span class="op">=></span> <span class="qr">'</span><span class="str">qr</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">String</span><span class="qr">'</span>            <span class="op">=></span> <span class="qr">'</span><span class="str">str</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Comment_Normal</span><span class="qr">'</span>    <span class="op">=></span> <span class="qr">'</span><span class="str">cm</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Comment_POD</span><span class="qr">'</span>       <span class="op">=></span> <span class="qr">'</span><span class="str">cmp</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Bareword</span><span class="qr">'</span>          <span class="op">=></span> <span class="qr">'</span><span class="str">bw</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Package</span><span class="qr">'</span>           <span class="op">=></span> <span class="qr">'</span><span class="str">pk</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Number</span><span class="qr">'</span>            <span class="op">=></span> <span class="qr">'</span><span class="str">nb</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Operator</span><span class="qr">'</span>          <span class="op">=></span> <span class="qr">'</span><span class="str">op</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Symbol</span><span class="qr">'</span>            <span class="op">=></span> <span class="qr">'</span><span class="str">sym</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Keyword</span><span class="qr">'</span>           <span class="op">=></span> <span class="qr">'</span><span class="str">kw</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Builtin_Operator</span><span class="qr">'</span>  <span class="op">=></span> <span class="qr">'</span><span class="str">bo</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Builtin_Function</span><span class="qr">'</span>  <span class="op">=></span> <span class="qr">'</span><span class="str">bf</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Character</span><span class="qr">'</span>         <span class="op">=></span> <span class="qr">'</span><span class="str">char</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Directive</span><span class="qr">'</span>         <span class="op">=></span> <span class="qr">'</span><span class="str">dr</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Label</span><span class="qr">'</span>             <span class="op">=></span> <span class="qr">'</span><span class="str">lb</span><span class="qr">'</span><span class="op">,</span>
    <span class="qr">'</span><span class="str">Line</span><span class="qr">'</span>              <span class="op">=></span> <span class="qr">'</span><span class="str">ln</span><span class="qr">'</span><span class="op">,</span>
<span class="sym">}</span><span class="sym">;</span>

<span class="kw">my</span> <span class="vs">$formatter</span> <span class="op">=</span> <span class="bw">Syntax::Highlight::Perl</span><span class="op">-></span><span class="sub">new</span><span class="sym">(</span><span class="sym">)</span><span class="sym">;</span>

<span class="vs">$formatter</span><span class="op">-></span><span class="sub">define_substitution</span><span class="sym">(</span><span class="qr">'</span><span class="str"><</span><span class="qr">'</span> <span class="op">=></span> <span class="qr">'</span><span class="str"><code>&lt;</code></span><span class="qr">'</span><span class="op">,</span> 
                                <span class="qr">'</span><span class="str">></span><span class="qr">'</span> <span class="op">=></span> <span class="qr">'</span><span class="str"><code>></code></span><span class="qr">'</span><span class="op">,</span> 
                                <span class="qr">'</span><span class="str">&</span><span class="qr">'</span> <span class="op">=></span> <span class="qr">'</span><span class="str"><code>&</code></span><span class="qr">'</span><span class="sym">)</span><span class="sym">;</span> <span class="cm"># HTML escapes.</span>

<span class="cm"># install the formats set up above</span>
<span class="kw">while</span> <span class="sym">(</span> <span class="kw">my</span> <span class="sym">(</span> <span class="vs">$type</span><span class="op">,</span> <span class="vs">$class</span> <span class="sym">)</span> <span class="op">=</span> <span class="bo">each</span> <span class="vh">%</span><span class="sym">{</span><span class="vs">$color_table</span><span class="sym">}</span> <span class="sym">)</span> <span class="sym">{</span>
	<span class="vs">$formatter</span><span class="op">-></span><span class="sub">set_format</span><span class="sym">(</span><span class="vs">$type</span><span class="op">,</span> <span class="sym">[</span> <span class="qr">qq~</span><span class="str"><code>&lt;span class=\"$class\"></code></span><span class="qr">~</span><span class="op">,</span> <span class="qr">'</span><span class="str"><code>&lt;/span></code></span><span class="qr">'</span> <span class="sym">]</span> <span class="sym">)</span><span class="sym">;</span>
<span class="sym">}</span>

<span class="bo">print</span> <span class="qr">qq~</span><span class="str"><code>&lt;link rel="stylesheet" href="highlightperl.css" type="text/css" /></code></span><span class="qr">~</span><span class="sym">;</span>
<span class="bo">print</span> <span class="qr">'</span><span class="str"><code>&lt;pre></code></span><span class="qr">'</span><span class="sym">;</span>
<span class="kw">while</span> <span class="sym">(</span><span class="op"><</span><span class="bw">DATA</span><span class="op">></span><span class="sym">)</span> <span class="sym">{</span>
    <span class="bo">print</span> <span class="vs">$formatter</span><span class="op">-></span><span class="sub">format_string</span><span class="sym">;</span>
<span class="sym">}</span>
<span class="bo">print</span> <span class="qr">"</span><span class="str"><code>&lt;/pre></code></span><span class="qr">"</span><span class="sym">;</span>
</pre></div>
<p><&lt;Previous: <a href="Module.html">Module</a>&nbsp;&nbsp;>>Next: <a href="ChineseCoding.html">汉字编码笔记</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=%E5%A6%82%E4%BD%95%E5%B0%86Perl%E4%BB%A3%E7%A0%81%E7%9D%80%E8%89%B2&url=http://www.fayland.org/journal/Highlight.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='Javascript_highlight_Perl.html'>Javascript Code Syntax Highlighter / Perl</a> < <span class='digit'>2005-11-08 01:37:28</span> ></li><li><a href='PerlTidy.html'>PerlTidy</a> < <span class='digit'>2004-10-30 19:23:26</span> ></li><li><a href='ask_tim.html'>Perl 6 什么时候才能完成？</a> < <span class='digit'>2004-12-30 18:42:06</span> ></li><li><a href='Perl6_ES14.html'>has $.Examples14 handles 'Perl6';</a> < <span class='digit'>2005-06-02 01:38:59</span> ></li><li><a href='050312.html'>给 eclipse 安装 EPIC plug-in</a> < <span class='digit'>2005-03-12 20:56:04</span> ></li><li><a href='050401.html'>Win32 下 Apache 和 #!/usr/bin/perl</a> < <span class='digit'>2005-04-01 10:10:59</span> ></li><li><a href='050519.html'>Synopsis localization</a> < <span class='digit'>2005-05-19 11:21:25</span> ></li><li><a href='Perl6_ES0.html'>@Examples[0] is Perl6</a> < <span class='digit'>2005-05-19 20:26:08</span> ></li><li><a href='Perl6_ES1.html'>given @Examples[1] when Perl6</a> < <span class='digit'>2005-05-19 22:05:56</span> ></li><li><a href='Perl6_ES2.html'>@Perl6 <== @Examples xx 2</a> < <span class='digit'>2005-05-20 18:53:54</span> ></li><li><a href='Perl6_ES3.html'>my Perl6 @Examples[3]</a> < <span class='digit'>2005-05-21 13:53:16</span> ></li><li><a href='Perl6_ES4.html'>say q:2 '@*Examples.[4] &Perl6()';</a> < <span class='digit'>2005-05-22 12:24:12</span> ></li><li><a href='Perl6_ES5.html'>sub Perl6 (*@Examples[5] is copy)</a> < <span class='digit'>2005-05-22 15:37:26</span> ></li><li><a href='Perl6_ES6.html'>multi sub Examples (6, Perl6) {...}</a> < <span class='digit'>2005-05-23 22:43:33</span> ></li><li><a href='Perl6_ES7.html'>sub infix:<(7)> ($Perl6, @Examples)</a> < <span class='digit'>2005-05-24 02:16:42</span> ></li><li><a href='Perl6_ES8.html'>let @Examples[8] = 'Perl6';</a> < <span class='digit'>2005-05-24 13:05:58</span> ></li><li><a href='050524.html'>The Shanghai PerlMonger meeting</a> < <span class='digit'>2005-05-24 20:41:25</span> ></li><li><a href='Perl6_ES9.html'>Perl6 | Examples & 9 ^ junction</a> < <span class='digit'>2005-05-26 12:35:29</span> ></li><li><a href='Perl6_ES10.html'>%Examples{'IO'} = open('Perl6');</a> < <span class='digit'>2005-05-30 00:24:01</span> ></li><li><a href='Perl6_ES11.html'>class Examples11 is Perl6 { ... }</a> < <span class='digit'>2005-05-30 13:31:31</span> ></li><li><a href='Perl6_ES12.html'>$Perl6 = $class.bless(:Example<12>);</a> < <span class='digit'>2005-05-31 11:38:46</span> ></li><li><a href='Perl6_ES13.html'>role Example13 { # Perl6 }</a> < <span class='digit'>2005-05-31 18:22:03</span> ></li><li><a href='Perl6_ES16.html'>my subtype Examples16 of Perl6 where /^kissu$/</a> < <span class='digit'>2005-06-04 23:59:29</span> ></li><li><a href='Perl6_ES17.html'>my enum Example17 <<:Perl(6) Today>></a> < <span class='digit'>2005-06-05 12:14:08</span> ></li><li><a href='Perl6_ES18.html'>my $queen; rule me {Perl6|Examples18}</a> < <span class='digit'>2005-06-06 12:26:32</span> ></li><li><a href='Perl6_ES19.html'>m:nth(19)/Perl6Examples/</a> < <span class='digit'>2005-06-06 22:02:01</span> ></li><li><a href='PBP3.html'>Tips from Perl Best Practices, Part 2</a> < <span class='digit'>2005-09-22 00:29:34</span> ></li><li><a href='Perl6_Rules.html'>用 Perl6::Rules 来写 Formatter</a> < <span class='digit'>2005-09-27 10:02:45</span> ></li><li><a href='mod_perl_learning.html'>a schedule of mod_perl learning</a> < <span class='digit'>2005-11-12 21:53:55</span> ></li><li><a href='mod_perl_configuring.html'>mod_perl 配置的一些指令</a> < <span class='digit'>2005-11-13 11:38:05</span> ></li><li><a href='modperl_helloworld.html'>modperl Apache2::HelloWorld</a> < <span class='digit'>2005-11-16 23:24:07</span> ></li><li><a href='pod2cn.html'>Perl 核心模块中文化</a> < <span class='digit'>2005-11-20 21:43:52</span> ></li><li><a href='modperl_http_cycle.html'>modperl 服务器的运行阶段和句柄</a> < <span class='digit'>2005-11-22 15:33:08</span> ></li><li><a href='modperl_PerlTransHandler.html'>modperl 的 PerlTransHandler 应用</a> < <span class='digit'>2005-11-23 22:24:02</span> ></li><li><a href='modperl_PerlAuthenHandler.html'>modperl 的用户验证</a> < <span class='digit'>2005-11-26 00:08:37</span> ></li><li><a href='modperl_Filter_part1.html'>modperl Filter Part1</a> < <span class='digit'>2005-12-18 14:47:29</span> ></li><li><a href='perldoc_ie.html'>用 IE 来浏览 perldoc</a> < <span class='digit'>2005-12-28 13:53:02</span> ></li></ul>
Created on <span class="digit">2004-11-20 22:13:22</span>, Last modified on <span class="digit">2004-11-21 17:53:17</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>